<?php

namespace App\Http\Controllers\Admin;

use App\Model\Permission;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class PermissionController extends Controller
{
    /**
     * 权限规则列表页面
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //接收表单数据
        $info = $request->all();
        // dd($info);

        //分页、多条件查询
        $permission = Permission::orderBy('id','asc')
            ->where(function($query) use($request){
                $per_name = $request->input('per_name');
                $cateid = $request->input('cateid');
                $contrller = $request->input('contrller');
                $action = $request->input('action');
                if (!empty($per_name)) {
                    # code...
                    $query->where('per_name','like','%'.$per_name.'%');
                }
                if (!empty($cateid)) {
                    # code...
                    $query->where('per_name','like','%'.$cateid.'%');
                }
                if (!empty($contrller)) {
                    # code...
                    $query->where('per_url','like','%'.$contrller.'%');
                }
                if (!empty($action)) {
                    # code...
                    $query->where('per_url','like','%'.$action.'%');
                }
            })->paginate($request->input('num')?$request->input('num'):5);

        //ID自增
        $i = 1;

        return view('admin.permission.list',compact('permission','request','i'));
    }

    /**
     * 权限规则添加页面
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('admin.permission.create');
    }

    /**
     * 接收表单数据，存储到数据库
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //接收表单数据
        $info = $request->all();
        //存储到数据库
        $res = Permission::insert($info,true);
        //判断执行结果，将状态信息返回至页面
        if ($res) {
            # code...
            $data = [
                'status'=>0,
                'message'=>'添加成功'
            ];
        } else {
            $data = [
                'status'=>1,
                'message'=>'添加失败'
            ];
        }
        return $data;
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * 权限规则修改页面
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //接收ID值，查询相关记录
        $permission = Permission::find($id);
        
        return view('admin.permission.edit',compact('permission'));
    }

    /**
     * 接收表单数据，更新数据库数据
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //接收表单数据
        $info = $request->all();
        //根据ID值更新数据库数据
        $res = Permission::where('id', $id)
            ->update($info,true);

        //判断执行结果，将状态返回至页面
        if ($res) {
            # code...
            $data = [
                'status' => 0,
                'message' => '修改成功'
            ];
        }else{
            $data = [
                'status' => 1,
                'message' => '修改失败'
            ];
        }
        return $data;
    }

    /**
     * 删除权限规则
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //根据ID值删除相关记录
        $res = Permission::where('id', $id)
            ->delete();
        //判断执行结果，将状态返回至页面
        if ($res) {
            # code...
            $data = [
                'status' => 0,
                'message' => '删除成功'
            ];
        }else{
            $data = [
                'status' => 1,
                'message' => '删除失败'
            ];
        }
        return $data;
    }

    /**
     * 批量删除权限规则
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function delAll(Request $request)
    {
        //接收选中的ID集合
        $ids = $request->input('ids');
        //根据所接收的ID，删除相关记录
        $res = Permission::where('id',$ids)->delete();
        //判断执行结果，将状态返回至页面
        if ($res) {
            # code...
            $data = [
                'status' => 0,
                'message' => '删除成功'
            ];
        }else{
            $data = [
                'status' => 1,
                'message' => '删除失败'
            ];
        }
        return $data;
    }
}
